<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>xeokit Example</title>
    <link href="../css/pageStyle.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/js/all.min.js"></script>
    <style>
        #downloadLink {
            position: absolute;
            font-size: 18px;
            left: 75px;
            top: 80px;
            pointer-events: all;
        }
    </style>
</head>
<body>
<input type="checkbox" id="info-button"/>
<label for="info-button" class="info-button"><i class="far fa-3x fa-question-circle"></i></label>
<canvas id="myCanvas"></canvas>
<div id="downloadLink"></div>
<div class="slideout-sidebar">
    <img class="info-icon" src="../../assets/images/bcf_logo.png"/>
    <h1>BCFViewpointsPlugin</h1>
    <h2>Loading a BCF Viewpoint saved with "defaultVisible" option</h2>
    <p>The BIM Collaboration Format (BCF) is a structured file format used for issue tracking with a building
        information model. BCF defines views of a building model and associated information on collisions
        and errors connected with specific objects in the view. </p>
    <p>In this example, we're loading a BIM model from the file system, then loading a JSON-encoded BCF viewpoint to
        highlight a simulated issue with our model.</p>
    <h3>Components Used</h3>
    <ul>
        <li>
            <a href="../../docs/class/src/viewer/Viewer.js~Viewer.html"
               target="_other">Viewer</a>
        </li>
        <li>
            <a href="../../docs/class/src/plugins/XKTLoaderPlugin/XKTLoaderPlugin.js~XKTLoaderPlugin.html"
               target="_other">XKTLoaderPlugin</a>
        </li>
        <li>
            <a href="../../docs/class/src/plugins/BCFViewpointsPlugin/BCFViewpointsPlugin.js~BCFViewpointsPlugin.html"
               target="_other">BCFViewpointsPlugin</a>
        </li>
    </ul>
    <h3>Tutorials</h3>
    <ul>
        <li>
            <a href="https://www.notion.so/xeokit/Saving-and-Loading-BCF-Viewpoints-800a5479dc8346848165c88b9ec84fcf" target="_other">Saving and Loading BCF Viewpoints</a>
        </li>
    </ul>
    <h3>Assets</h3>
    <ul>
        <li>
            <a href="https://github.com/openBIMstandards/DataSetSchependomlaan"
               target="_other">Model source</a>
        </li>
    </ul>
</div>

<script type="module">

    //------------------------------------------------------------------------------------------------------------------
    // Import the modules we need for this example
    //------------------------------------------------------------------------------------------------------------------

    import {Viewer, XKTLoaderPlugin, BCFViewpointsPlugin} from "../../dist/xeokit-sdk.min.es.js";

    //------------------------------------------------------------------------------------------------------------------
    // Create a Viewer
    //------------------------------------------------------------------------------------------------------------------

    const viewer = new Viewer({
        canvasId: "myCanvas",
        transparent: true
    });

    const camera = viewer.camera;

    camera.eye = [-2.37, 18.97, -26.12];
    camera.look = [10.97, 5.82, -11.22];
    camera.up = [0.36, 0.83, 0.40];

    //------------------------------------------------------------------------------------------------------------------
    // Add a XKTModelsPlugin - we'll use this to load the model geometry and IFC metadata
    //------------------------------------------------------------------------------------------------------------------

    const xktLoader = new XKTLoaderPlugin(viewer);

    //------------------------------------------------------------------------------------------------------------------
    // Add a BCFViewpointsPlugin - we'll use this to load the BCF viewpoint
    //------------------------------------------------------------------------------------------------------------------

    const bcfViewpoints = new BCFViewpointsPlugin(viewer);

    //------------------------------------------------------------------------------------------------------------------
    // Load two .xkt models and their IFC metadata
    //------------------------------------------------------------------------------------------------------------------

    const sceneModel = xktLoader.load({
        id: "myModel",
        src: "../../assets/models/xkt/v8/ifc/Schependomlaan.ifc.xkt",
        edges: true // Emphasise edges
    });

    const sceneModel2 = xktLoader.load({
        id: "mysceneModel2",
        src: "../../assets/models/xkt/v10/glTF-Embedded/Duplex_A_20110505.glTFEmbedded.xkt",
        edges: true
    });

    //------------------------------------------------------------------------------------------------------------------
    // When the model has loaded, load the BCF viewpoint
    //
    // The BCF viewpoint loads the camera position, creates a clipping plane, and selects several objects
    //------------------------------------------------------------------------------------------------------------------

    sceneModel.on("loaded", () => {
        sceneModel2.on("loaded", () => {
            bcfViewpoints.setViewpoint(getBCFViewpoint());
        });
    });

    //------------------------------------------------------------------------------------------------------------------
    // Create link to download our JSON BCF viewpoint
    // https://stackoverflow.com/questions/2897619/using-html5-javascript-to-generate-and-save-a-file
    //------------------------------------------------------------------------------------------------------------------

    // Create download link
    // https://stackoverflow.com/questions/2897619/using-html5-javascript-to-generate-and-save-a-file

    const pom = document.createElement('a');

    pom.onclick = function () {

        const viewpoint = bcfViewpoints.getViewpoint({ // Options
            spacesVisible: false, // Don't force IfcSpace types visible in viewpoint (default)
            spaceBoundariesVisible: false, // Don't show IfcSpace boundaries in viewpoint (default)
            openingsVisible: false // Don't force IfcOpening types visible in viewpoint (default)
        });

        const viewpointStr = JSON.stringify(viewpoint, null, 4);
        pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(viewpointStr));
    }

    pom.setAttribute('download', "bcfViewpoint.json");
    pom.innerText = "Download BCF Viewpoint JSON";
    document.getElementById("downloadLink").appendChild(pom);

    //------------------------------------------------------------------------------------------------------------------
    // Returns the JSON BCF viewpoint we'll load
    //------------------------------------------------------------------------------------------------------------------

    function getBCFViewpoint() {
        return {
            "perspective_camera": {
                "camera_view_point": {
                    "x": -2.37,
                    "y": 26.12,
                    "z": 18.97
                },
                "camera_direction": {
                    "x": 0.5573409184967458,
                    "y": -0.622517217811208,
                    "z": -0.5494027794776767
                },
                "camera_up_vector": {
                    "x": 0.36,
                    "y": -0.4,
                    "z": 0.83
                },
                "field_of_view": 60
            },
            "clipping_planes": [
                {
                    "location": {
                        "x": 0,
                        "y": 0,
                        "z": 0
                    },
                    "direction": {
                        "x": 0.5,
                        "y": -0.5,
                        "z": 0
                    }
                }
            ],
            "components": {
                "visibility": {
                    "view_setup_hints": {
                        "spaces_visible": false,
                        "space_boundaries_visible": false,
                        "openings_visible": false
                    },
                    "exceptions": [],
                    "default_visibility": true
                },
                "coloring": [],
                "selection": [
                    {
                        "ifc_guid": "3b2U496P5Ebhz5FROhTwFH",
                        "originating_system": "xeokit.io"
                    },
                    {
                        "ifc_guid": "2MGtJUm9nD$Re1_MDIv0g2",
                        "originating_system": "xeokit.io"
                    },
                    {
                        "ifc_guid": "3IbuwYOm5EV9Q6cXmwVWqd",
                        "originating_system": "xeokit.io"
                    },
                    {
                        "ifc_guid": "3lhisrBxL8xgLCRdxNG$2v",
                        "originating_system": "xeokit.io"
                    },
                    {
                        "ifc_guid": "1uDn0xT8LBkP15zQc9MVDW",
                        "originating_system": "xeokit.io"
                    }
                ]
            },
            "snapshot": {
                "snapshot_type": "png",
                "snapshot_data": ""
            }
        };
    }
</script>

</html>